//
//  ZKVisitorView.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/23.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit

//导入第三方框架
import SnapKit

//设置代理协议,不使用NSObjectProtocol,就不能使用weak关键字
protocol ZKVisitorViewDelegate:NSObjectProtocol{
    
    //设置代理方法
    func loginOperation()
    
}
class ZKVisitorView: UIView {
    
    //点击注册或登录执行的闭包
    var loginCallBack: (() -> ())?
    
    //接收代理对象,一般使用weak关键字,确保不会被强引用
    weak var delegate: ZKVisitorViewDelegate?
    
    //懒加载控件
    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //罩层图片
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //visitordiscover_feed_image_house
    private lazy var homeIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字闭包
    private lazy var textLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
        
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("注册", for: .normal)
        button.addTarget(self, action: #selector(clickRegisterButton), for: .touchUpInside)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //拉伸图片
       // let image = UIImage(named: "common_button_white_disable")?.stretchableImage(withLeftCapWidth: 5, topCapHeight: 5)
        //resizableImageWithCapInsets
        let edgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: edgeInsets)
        button.setBackgroundImage(image, for: .normal)
        //取消点击高亮
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", for: .normal)
        button.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //拉伸图片
        // let image = UIImage(named: "common_button_white_disable")?.stretchableImage(withLeftCapWidth: 5, topCapHeight: 5)
        //resizableImageWithCapInsets
        let edgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: edgeInsets)
        button.setBackgroundImage(image, for: .normal)
        //取消点击高亮
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()

 
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: -------添加控件并设置约束-------
    
    private func setupUI(){
    
    
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        //添加子控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeIconImageView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //使用SnapKit添加约束
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        homeIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(maskIconImageView)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp.bottom)
            
            make.centerX.equalTo(cycleImageView)
            
            make.width.equalTo(225)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.leading.equalTo(textLabel)
            
            make.top.equalTo(textLabel.snp.bottom).offset(10)
            
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(registerButton)
            
            make.trailing.equalTo(textLabel)
            
            make.size.equalTo(registerButton)
        }
        
    
        
        
    }
    
    //开启旋转动画
    private func startAnimation(){
        
       let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //动画时间
        animation.duration = 20
        //动画旋转的度数
        animation.toValue = 2 * Double.pi
        //动画执行的次数
        animation.repeatCount = MAXFLOAT
        //失去焦点时不释放动画
        animation.isRemovedOnCompletion = false
        //添加动画
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    

    //修改访客视图信息
    func updateVisitorInfo(text:String?, imageName:String?){
        
        if let text = text,let imgName = imageName{
            
            //表示不是首页
            cycleImageView.isHidden = true
            textLabel.text = text
            homeIconImageView.image = UIImage(named: imgName)
            
        }else{
            
           //首页
            startAnimation()
            
        
        }
    }
    
    //MARK: -------点击注册和登录按钮执行的事件-------
   @objc private func clickRegisterButton(){
    
        //执行闭包完成回调
        loginCallBack?()
    
        //代理方法回调
        delegate?.loginOperation()
    
    
        
    }
    
    @objc private func clickLoginButton(){
    
        loginCallBack?()
        
        delegate?.loginOperation()
    }
}

